package it.asvp.asvp_portal.service.services.impl;

import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.dao.MemberDao;
import it.asvp.asvp_portal.data.dao.NewsDao;
import it.asvp.asvp_portal.data.dao.NewsPictureDao;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.data.entity.news.News;
import it.asvp.asvp_portal.data.entity.news.NewsPicture;
import it.asvp.asvp_portal.service.services.MemberAccountService;
import it.asvp.asvp_portal.service.services.NewsService;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

@Service
public class NewsServiceImpl implements  NewsService{

	@Autowired
	NewsDao newsDao;
	@Autowired
	NewsPictureDao newsPictureDao;
	@Autowired
	MemberDao memberDao;
	@Autowired
	MemberAccountService memberAccountService;
	
	@Transactional
	@Rollback
	public Long createNews(Long idCurrentAccount,News transientNews) throws Exception{
		
		transientNews.setCreatedOn(DateUtils.getCurrentDate());
		transientNews.setLastUpdate(DateUtils.getCurrentDate());
		
		Member persistentMember=memberDao.findById(idCurrentAccount);
		transientNews.setMember(persistentMember);
		return newsDao.persist(transientNews);
	}
	
	@Transactional
	@Rollback
	public void updateNews(Long idCurrentAccount, Long idNewsToUpdate, News transientNews) throws Exception{
		
		News persistentNews=newsDao.findById(idNewsToUpdate);
		
		if(persistentNews==null){
			throw new Exception("Notizia non presente");
		}
		
		updatePictures(persistentNews.getId(),persistentNews.getNewsPictures(),transientNews.getNewsPictures());	
		
		persistentNews.setNewsType(transientNews.getNewsType());
		persistentNews.setLastUpdate(DateUtils.getCurrentDate());
		persistentNews.setLink(transientNews.getLink());
		persistentNews.setPriority(transientNews.isPriority());
		persistentNews.setText(transientNews.getText());
		persistentNews.setTitle(transientNews.getTitle());
		
        newsDao.merge(persistentNews);
	}
	
	@Transactional
	@Rollback
	public void deleteNews(Long idCurrentAccount, Long idNewsToDelete) throws Exception{
		
			News persistentNews=newsDao.findById(idNewsToDelete);
			
			if(persistentNews==null){
				throw new Exception("Notizia non presente");
			}
			
			newsDao.delete(persistentNews);	
	}
	
	@Transactional
	@Rollback
	public List<News> getAllNews(){
		
		List<News> allNews=newsDao.findAll();
		return allNews;
	}
	
	@Transactional
	@Rollback
	public News getById(Long idNews) throws Exception{
		
		  News persistentNews=newsDao.findById(idNews);
		  if(persistentNews==null){
			  throw new Exception("Notizia non presente");
		  }
		  return persistentNews;
	} 
	
	@Transactional
	@Rollback
	private void updatePictures(Long idNews, Set<NewsPicture> oldNewsPictures,Set<NewsPicture> newNewsPictures){
		
		for(NewsPicture newsPicture:oldNewsPictures){
			newsPictureDao.delete(newsPicture);
		}
		
		for(NewsPicture newsPicture:newNewsPictures){
			newsPicture.setId(idNews);
			newsPictureDao.persist(newsPicture);
		}
	}
}
